package com.hrbxlh.virtualization.listener;

import cn.hutool.core.date.DateUtil;
import com.hrbxlh.virtualization.constant.RancherConstant;
import com.hrbxlh.virtualization.dao.ContainerMapper;
import com.hrbxlh.virtualization.enums.ContainerStatusEnum;
import com.hrbxlh.virtualization.pojo.Container;
import com.hrbxlh.virtualization.pojo.ContainerExample;
import com.hrbxlh.virtualization.service.DockerService;
import com.hrbxlh.virtualization.support.SpringContextHolder;
import com.hrbxlh.virtualization.websocket.DesktopWebSocket;

import java.util.List;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author cheer
 */
public class ContainerStopDelayTask implements Delayed, Runnable {

    private static ContainerMapper containerMapper = SpringContextHolder.getBean(ContainerMapper.class);

    private static DockerService dockerService = SpringContextHolder.getBean(DockerService.class);

    /**
     * 用户Id
     */
    private String stuorteaId;

    private Integer courseId;

    /**
     * 章节Id
     */
    private Integer chapterId;

    /**
     * 过期时间，单位：秒
     */
    private Long expireTime;

    /**
     * @param stuorteaId 用户Id
     * @param chapterId  章节Id
     */
    public ContainerStopDelayTask(String stuorteaId, Integer courseId, Integer chapterId) {
        this.stuorteaId = stuorteaId;
        this.courseId = courseId;
        this.chapterId = chapterId;
        this.expireTime = DateUtil.currentSeconds() + Long.valueOf(RancherConstant.STOP_DELAY);
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(expireTime - DateUtil.currentSeconds(), TimeUnit.SECONDS);
    }

    @Override
    public void run() {
        if(!DesktopWebSocket.isOnline(stuorteaId, courseId, chapterId)){
            ContainerExample containerExample = new ContainerExample();
            containerExample.or().andStuorteaIdEqualTo(stuorteaId)
                    .andCourseIdEqualTo(courseId)
                    .andChapterIdEqualTo(chapterId);
            List<Container> containerList = containerMapper.selectByExample(containerExample);
            containerList.forEach(container -> {
                dockerService.stopContainer(container.getRancherId());
                container.setContainerStatus(ContainerStatusEnum.STOPPED.getCode());
                containerMapper.updateByPrimaryKeySelective(container);
            });
        }
    }

    @Override
    public int compareTo(Delayed o) {
        if (o == this) {
            return 0;
        }
        if (o instanceof ContainerStopDelayTask) {
            ContainerStopDelayTask other = (ContainerStopDelayTask) o;
            if (expireTime < other.expireTime) {
                return -1;
            } else if (expireTime > other.expireTime) {
                return 1;
            }
        }
        return 0;
    }

    public String getStuorteaId() {
        return stuorteaId;
    }

    public void setStuorteaId(String stuorteaId) {
        this.stuorteaId = stuorteaId;
    }

    public Integer getCourseId() {
        return courseId;
    }

    public void setCourseId(Integer courseId) {
        this.courseId = courseId;
    }

    public Integer getChapterId() {
        return chapterId;
    }

    public void setChapterId(Integer chapterId) {
        this.chapterId = chapterId;
    }
}
